package pojo;

import lombok.Data;
import tool.LogCat;

import java.io.Serializable;
import java.util.Objects;

//时间类
@Data
public class Time implements Serializable {
    //年
    private int year;
    //月
    private int month;
    //日
    private int day;

    //全参构造
    public Time(int year, int month, int day) {
        this.year = year;
        this.month = month;
        this.day = day;
    }

    //重写equal方法
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Time time = (Time) o;
        return year == time.year && month == time.month && day == time.day;

    }
    @Override
    public int hashCode() {
        return Objects.hash(year, month, day);
    }

    //toString方法
    //要求:年-月-日
    //月和日如果没有两位则补充0
    @Override
    public String toString() {
        LogCat.d("执行Time的toString方法");
        String result = String.format("%04d-%02d-%02d", year, month, day);
        LogCat.d(result);
        return result;
    }

    //根据字符串构造方法
    public Time(String str) {
        String[] split = str.split("-");
        LogCat.d( split[0] + " " + split[1] + " " + split[2]);
        //去除前缀0
        for (int i = 0; i < split.length; i++) {
            split[i] = removePrefixZero(split[i]);
            LogCat.d("i=" + i + "  " + split[i]);
        }
        LogCat.d( split[0] + " " + split[1] + " " + split[2]);

        LogCat.d("test[2]"+split[2]);
        LogCat.printStringWithCharsAndCodes(split[2]);

        this.year = Integer.parseInt(split[0]);
        LogCat.d("year成功");
        this.month = Integer.parseInt(split[1]);
        LogCat.d("month成功");
        LogCat.d("dayStr=:"+split[2]+"   this.day="+this.day);
        this.day = Integer.parseInt(split[2]);
        LogCat.d("day成功");
        LogCat.d(this+"");
    }

    //去除前缀0
    private String removePrefixZero(String str) {
        if (!str.startsWith("0")) {
            return str;
        }
        return removePrefixZero(str.substring(1));
    }
}
